// pages/dsPark/dsPark.js
var amapFile = require('../../utils/amap-wx.js');
var util = require('../../utils/request.js');
var globalData = getApp().globalData;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 地区编码
    adcode: 'none'
  },
  scanCode: function () {
    var that = this;
    wx.scanCode({
      success: function (res) {
        console.log(res);
        // console.log('1111&.vb'.indexof('&'));v
        var code = res.result
        console.log(code);
        var uid = code.indexOf('uid=');
        uid = code.slice(uid + 4, uid + 9);
        var area = code.slice(-4);
        uid = area + uid;
        console.log(uid);
        wx.showModal({
          title: '提示',
          content: '为确保您的车位不被他人占用，请将您的爱车驾驶至车位前，再进行解锁！',
          success: (data) => {
            console.log(data);
            if(data.confirm) {
              wx.showLoading({
                title: '开锁中...',
                mask: true
              })
              util.req('/order/use', { 'uid': uid, 'memberId': globalData.memberId }, function (res) {
                console.log(res);
                wx.hideLoading();
                if (res.resultCode == 200) {
                  wx.showToast({
                    title: '开锁成功',
                  })
                  setTimeout(function () {
                    wx.reLaunch({
                      url: '../index/index',
                    })
                  }, 500)
                } else {
                  wx.showToast({
                    title: res.resultMsg,
                    icon: 'none'
                  })
                }
              })
            }
          }
        })
      }
    })
  },
  CodeLock: function () {
    if(this.data.adcode == 'none') {
      wx.showToast({
        title: '该地区暂未开通运营，敬请期待！',
        icon: 'none'
      })
    } else {
      wx.navigateTo({
        url: '../parkCode/parkCode?adcode=' + this.data.adcode
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var myAmapFun = new amapFile.AMapWX({ key: '14c7d5a7fe1f44284eabe3eb47800d59' });
    myAmapFun.getRegeo({
      success: function (data) {
        //成功回调
        console.log(data);
        var code = data[0].regeocodeData.addressComponent.adcode;
        console.log(code);
        util.req('/device/area', { 'areaId': code }, function (res) {
          console.log(res);
          if (res.resultCode == 200) {
            that.setData({
              adcode: res.resultContent.deviceCode
            })
          } else {
            that.setData({
              adcode: 'none'
            })
          }
        })
      },
      fail: function (info) {
        //失败回调
        console.log(info)
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})